import time

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import re

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.status = 0
        self.flag = 0

    def setupUi(self, MainWindow):
        self.centralwidget = MainWindow
        self.centralwidget.setWindowTitle("Network Debug Assistant")
        self.centralwidget.setGeometry(500, 350, 800, 510)

        self.label_protocol = QLabel(self.centralwidget)
        self.label_protocol.setGeometry(20, 20, 80, 30)
        self.label_protocol.setText("协议")

        self.comboBox_protocol = QComboBox(self.centralwidget)
        self.comboBox_protocol.setGeometry(120, 20, 120, 30)
        # self.comboBox_protocol.addItems(['UDP Server', 'UDP Client', 'TCP Server', 'TCP Client', ])
        self.comboBox_protocol.addItems(['UDP', 'TCP Server', 'TCP Client'])
        self.comboBox_protocol.currentIndexChanged.connect(self.protocol_changed)

        self.label_local = QLabel(self.centralwidget)
        self.label_local.setGeometry(20, 70, 80, 30)
        self.label_local.setText("本地主机")

        self.line_local = QLineEdit(self.centralwidget)
        self.line_local.setGeometry(120, 70, 120, 30)

        self.label_lport = QLabel(self.centralwidget)
        self.label_lport.setGeometry(20, 110, 80, 30)
        self.label_lport.setText("本地端口")

        self.line_lport = QLineEdit(self.centralwidget)
        self.line_lport.setGeometry(120, 110, 120, 30)

        self.label_dhost = QLabel(self.centralwidget)
        self.label_dhost.setGeometry(20, 150, 80, 30)
        self.label_dhost.setText("目的主机")

        self.line_dhost = QLineEdit(self.centralwidget)
        self.line_dhost.setGeometry(120, 150, 120, 30)

        self.label_dport = QLabel(self.centralwidget)
        self.label_dport.setGeometry(20, 200, 80, 30)
        self.label_dport.setText("目的端口")

        self.line_dport = QLineEdit(self.centralwidget)
        self.line_dport.setGeometry(120, 200, 120, 30)

        self.btn_connect = QPushButton(self.centralwidget)
        self.btn_connect.setGeometry(20, 250, 100, 30)
        self.btn_connect.setText("打开")
        self.btn_connect.setIcon(QIcon("connect.svg"))
        self.btn_connect.clicked.connect(self.btn_connect_changed)

        self.btn_savelog = QPushButton(self.centralwidget)
        self.btn_savelog.setGeometry(140, 250, 100, 30)
        self.btn_savelog.setText("导出日志")

        self.label_datarecv = QLabel(self.centralwidget)
        self.label_datarecv.setGeometry(300, 20, 100, 30)
        self.label_datarecv.setText("数据显示区")

        self.text_datarecv = QTextBrowser(self.centralwidget)
        self.text_datarecv.setGeometry(300, 50, 480, 300)

        self.label_datasend = QLabel(self.centralwidget)
        self.label_datasend.setGeometry(300, 350, 100, 30)
        self.label_datasend.setText("数据发送区")

        self.text_datasend = QTextEdit(self.centralwidget)
        self.text_datasend.setGeometry(300, 380, 480, 80)

        self.btn_send = QPushButton(self.centralwidget)
        self.btn_send.setGeometry(300, 470, 120, 30)
        self.btn_send.setText("发送")
        self.btn_send.setIcon(QIcon("send.svg"))

        self.btn_clearrecv = QPushButton(self.centralwidget)
        self.btn_clearrecv.setGeometry(520, 470, 120, 30)
        self.btn_clearrecv.setText("清空显示区域")

        self.btn_clearsend = QPushButton(self.centralwidget)
        self.btn_clearsend.setGeometry(660, 470, 120, 30)
        self.btn_clearsend.setText("清空发送区域")




    def btn_connect_changed(self):
        if not self.check():
            return False

        self.status = 1 - self.status
        if self.status == 1:

            self.btn_connect.setIcon(QIcon("disconnect.svg"))
            self.comboBox_protocol.setEnabled(False)
            self.line_local.setEnabled(False)
            self.line_lport.setEnabled(False)
            self.line_dhost.setEnabled(False)
            self.line_dport.setEnabled(False)
            if self.flag == 0:
                self.btn_connect.setText("关闭")
            else:
                self.btn_connect.setText("断开")

        else:

            self.btn_connect.setIcon(QIcon("connect.svg"))
            self.comboBox_protocol.setEnabled(True)
            self.line_local.setEnabled(True)
            self.line_lport.setEnabled(True)
            self.line_dhost.setEnabled(True)
            self.line_dport.setEnabled(True)
            if self.flag == 0:
                self.btn_connect.setText("打开")
            else:
                self.btn_connect.setText("连接")


    # 表单验证
    def check(self):
        if not self.line_lport.text():
            QMessageBox.information(self.centralwidget, "提示", "请输入本地端口")
            return False
        if not self.line_dhost.text():
            QMessageBox.information(self.centralwidget, "提示", "请输入目的主机")
            return False
        if not self.line_dport.text():
            QMessageBox.information(self.centralwidget, "提示", "请输入目的端口")
            return False

        pattern_ip = re.compile(r'\d+\.\d+\.\d+\.\d+$')
        pattern_port = re.compile(r'^\d+$')

        reg_lport = pattern_port.findall(self.line_lport.text())[0]
        reg_dport = pattern_port.findall(self.line_dport.text())[0]

        if not reg_lport:
            QMessageBox.information(self.centralwidget, "提示", "请输入合法的本地端口")
            return False

        if eval(reg_lport) > 65535:
            QMessageBox.information(self.centralwidget, "提示", "请输入合法的本地端口")
            return False

        if not pattern_ip.findall(self.line_dhost.text()):
            QMessageBox.information(self.centralwidget, "提示", "请输入合法的目的主机")
            return False

        if not reg_dport:
            QMessageBox.information(self.centralwidget, "提示", "请输入合法的目的端口")
            return False

        if eval(reg_dport) > 65535:
            QMessageBox.information(self.centralwidget, "提示", "请输入合法的目的端口")
            return False
        return True

    def protocol_changed(self):
        protocols = {'UDP': 0, 'TCP Server': 1, 'TCP Client': 2}
        protocol_current = self.comboBox_protocol.currentText()
        self.flag = protocols[protocol_current]
        if self.flag == 0:
            self.btn_connect.setText("打开")
        else:
            self.btn_connect.setText("连接")












